package com.ruoyi.web.controller.surfacewater;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.excel.ExcelUtiles;
import com.ruoyi.drinkwater.domain.ManualParamVo;
import com.ruoyi.surfacewater.domain.ManualDataVo;
import com.ruoyi.surfacewater.service.BusMonthlycumulativedataService;
import com.ruoyi.surfacewater.service.ManualDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/manualData")
public class ManualDataController extends BaseController {

    @Autowired
    private ManualDataService manualDataService;
    @Autowired
    private BusMonthlycumulativedataService busMonthlycumulativedataService;

    /**
     * 手工数据查询
     */
    @PostMapping("/manualMonitorData")
    public TableDataInfo manualMonitorData(@RequestBody ManualDataVo vo) {
//        startPage();
        List<Map<String, Object>> list = manualDataService.manualMonitorData(vo);

        return getDataTable(list);
    }

    /**
     * 手工数据文件上传
     */
    @PostMapping("/excelFileShell")
    public Map<String, Object> excelFileShell(MultipartFile file) {

        Map maplist = new HashMap();
        String fileName = file.getOriginalFilename();
        if (!fileName.toLowerCase().endsWith(".xls") && !fileName.toLowerCase().endsWith(".xlsx")) {
            maplist.put("msg", "上传文件格式错误");
            maplist.put("code", 500);
            maplist.put("data", "");
        } else {
            ExcelUtiles excel = new ExcelUtiles();
            List<Map<String, Object>> excelFileShellmaps = excel.excelFileShell(file);
            maplist.put("msg", "查询成功");
            maplist.put("code", 200);
            maplist.put("data", excelFileShellmaps);
        }
        return maplist;
    }

    /**
     * 手工数据导入 进入临时表
     *
     * @param file      文件
     * @param sheetName sheet名称
     * @param prop      数据属性
     */
    @RequestMapping("/surfaceWaterListData")
    public Map<String, Object> surfaceWaterListData(MultipartFile file, String sheetName, String prop) {
        Map maplist = new HashMap();
        //判断文件是否存在
        // 获取文件原始名称
        // 获取文件原始名称
        String fileName = file.getOriginalFilename();
        if (!fileName.toLowerCase().endsWith(".xlsx")) {
            maplist.put("msg", "上传文件格式错误,请上传xlsx格式excel文件");
            maplist.put("code", 200);
            maplist.put("data", "");
            return maplist;
        } else {
            Map<String, Object> stringObjectMap = manualDataService.surfaceWaterListData(file, sheetName, prop);

            return stringObjectMap;
        }
        //return maplist;
    }

    /**
     * 查询导入临时表中的数据
     */
    @GetMapping("/busDataTempdataReportID")
    public Map<String, Object> busDataTempdataReportID(String datatype) {
        Map maplist = new HashMap();
        Map map = new HashMap();
        map.put("datatype", datatype);
        List<Map<String, Object>> list = manualDataService.wqDataTempdataReportID(map);
        maplist.put("msg", "查询成功");
        maplist.put("code", 200);
        maplist.put("data", list);
        return maplist;
    }

    /**
     * 导入数据详情
     *
     * @param reportId 数据id
     * @return
     */
    @GetMapping("/importTemplateData")
    public Map<String, Object> importTemplateData(String reportId) {
        Map map = new HashMap();
        map.put("ReportID", reportId);
        Map maplist = new HashMap();
        List list = manualDataService.importTemplateData(map);
        List titlelist = manualDataService.importTemplateTitle(map);
        maplist.put("msg", "查询成功");
        maplist.put("code", 200);
        maplist.put("data", list);
        maplist.put("title", titlelist);
        return maplist;
    }


    /**
     * 根据上传编码删除数据
     *
     * @Param [ReportID] 编码id
     **/
    @GetMapping("/deleteBusTempdataByReportId")
    public AjaxResult deleteBusTempdataByReportID(String reportId) {
        int i = manualDataService.deleteBusTempdataByReportID(reportId);
        return toAjax(i);
    }

    /**
     * 模板导入的数据根据操作入正式数据库
     *
     * @Param [ReportID 模板导入生成的唯一id,
     * operationType 操作类型
     * 0 覆盖:更新正式库
     * 1 保留:临时表的库删除 已经存在的状态不用操作 不存在的保存库里
     * segmentID 断面id多个以逗号隔开]
     **/
    @GetMapping("/dataReportingAndSaving")
    public Map<String, Object> dataReportingAndSaving(String reportId) {
        Map<String, Object> maplist = new HashMap<>();
        Map mapdata = new HashMap();
        mapdata.put("ReportID", reportId);
        int importDataCover = manualDataService.importDataCover(mapdata);
        if (importDataCover == 0) {
            maplist.put("msg", "数据正在入库，请稍后查看数据");
        } else if (importDataCover == 1) {
            maplist.put("msg", "导入数据缺少断面主键");
        }
        maplist.put("code", 200);
        maplist.put("data", importDataCover);
        return maplist;
    }


    /**
     * 模板导入的数据根据操作入正式数据库
     *
     * @Param [ReportID 模板导入生成的唯一id,
     * operationType 操作类型
     * 0 覆盖:更新正式库
     * 1 保留:临时表的库删除 已经存在的状态不用操作 不存在的保存库里
     * segmentID 断面id多个以逗号隔开]
     **/
    @RequestMapping("/dataReportingAndSavingsyd")
    public Map<String, Object> dataReportingAndSavingsyd(String reportID, String operationType, String segmentID) {
        Map<String, Object> maplist = new HashMap<>();
        Map mapdata = new HashMap();
        mapdata.put("ReportID", reportID);
        //断面id处理
        List<String> segmentIDs = null;
        if (segmentID != null && !"".equals(segmentID)) {
            String[] arr = segmentID.split(",");
            segmentIDs = Arrays.asList(arr);
            mapdata.put("segmentIds", segmentIDs);
        }
        //操作状态位0 覆盖:全部更新到正式库表中之后删除临时表数据
        int importDataCover = manualDataService.importDataCoversyd(mapdata);
        if (importDataCover == 0) {
            maplist.put("msg", "数据正在入库，请稍后查看数据");
        } else if (importDataCover == 1) {
            maplist.put("msg", "导入数据缺少断面主键");
        }

        maplist.put("code", 200);
        maplist.put("data", importDataCover);

        return maplist;
    }


    /**
     * 手工断面导入数据查询
     *
     * @return
     */
    @GetMapping("/getManualMonitorData")
    private TableDataInfo getManualMonitorData(String segmentId, String dataTime, Integer pageNum, Integer pageSize) {

        Map map = new HashMap(); //存放传入参数
        map.put("prop", "CBN");

        List<String> segmentids = null;
        if (segmentId != null && !"".equals(segmentId)) {
            String[] arr = segmentId.split(",");
            segmentids = Arrays.asList(arr);
            map.put("segmentids", segmentids);

        }
        map.put("dataTime", dataTime);
        //查询处理后的结果数据值
        List<Map<Object, Object>> manualWqDataBasicsmonitordata = manualDataService.getManualMonitorData(map);
        List<Map<Object, Object>> retList = PageUtil.startListPage(manualWqDataBasicsmonitordata, pageNum, pageSize).getList();

        return getDataTable(retList);
    }

    /**
     * 手工断面导入数据删除
     * @return
     */
    @PostMapping("/deleteManualMonitorData")
    public AjaxResult deleteManualMonitorData(@RequestBody  ManualParamVo vo) {
        List<String> segmentIdList=Arrays.asList(vo.getSegmentId().split(","));
        List<String> reportIdList=Arrays.asList(vo.getReportId().split(","));
        List<ManualParamVo> dvo=new ArrayList<>();
        int i=0;
        for(String segmentId:segmentIdList){
            ManualParamVo paramVo=new ManualParamVo();
            paramVo.setSegmentId(segmentId);
            paramVo.setReportId(reportIdList.get(i));
            paramVo.setSampleDate(vo.getSampleDate());
            paramVo.setBussinessCategoryCode(vo.getBussinessCategoryCode());
            dvo.add(paramVo);
            i++;
        }
        Integer integer = manualDataService.deleteMaunalSegment(dvo);
        return toAjax(integer);
    }




    @GetMapping("/getSegmentData")
    public Map<String, Object> getSegmentData() {
        busMonthlycumulativedataService.selectSegmentData();
        return null;
    }

}
